﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Gwn.Library.MvpVm.Interfaces;

namespace Gwn.Library.MvpVm.Base
{
    /// <summary>
    /// UserControl baseclass
    /// </summary>
    public class UserControlBase : UserControl, IUserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UserControlBase"/> class.
        /// </summary>
        public UserControlBase()
        {

        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public virtual void Initialize()
        {
            // Note: All three platforms use the same files.  The projects 
            // have been modified so that all three VS project files can
            // reside in the same folder (by providing each Properties with
            // a suffix, i.e., Desktop, Silverlight and Phone)
            // [more] http://www.global-webnet.net/blogengine/post/2011/09/02/Sharing-the-same-resources-between-Desktop-Silverlight-and-Phone.aspx
            var rd = new ResourceDictionary();

#if GWNPHONE
            rd.Source = new Uri("/Gwn.Library.Resources.Phone;component/Gwn.Library.Resources.Generic.xaml", UriKind.Relative);
#endif

#if GWNDESKTOP
            rd.Source = new Uri("/Gwn.Library.Resources.Desktop;component/Gwn.Library.Resources.Generic.xaml", UriKind.Relative);
#endif

#if GWNSILVERLIGHT
            rd.Source = new Uri("/Gwn.Library.Resources.Silverlight;component/Gwn.Library.Resources.Generic.xaml", UriKind.Relative);
#endif

            Resources.MergedDictionaries.Add(rd);
        }

        /// <summary>
        /// Gets the view model holder.  Implements IViewModelHolder
        /// Gwn.Library.Resources.DataTemplates.xaml holds the
        /// DataTemplate for the ViewModels.  Since the Phone
        /// and Silverlight do not support DataType we had to go with
        /// a control that would support DataTemplates on all three 
        /// platforms.  The ViewModelHolder is an ItemsControl so we
        /// have to add the ViewModel to it; it is available via
        /// DataContext as well as in ItemsControl (for template)
        /// </summary>
        /// <value>The view model holder.</value>
        public ItemsControl ViewModel
        {
            get
            {
                return (ItemsControl) FindName("ViewModelHolder");
            }
        }

        /// <summary>
        /// Finds the name.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="controlName">Name of the control.</param>
        /// <returns></returns>
        public T FindChildName<T>(string controlName)
            where T : DependencyObject
        {
            var control = FindVisualChild<T>(this);
            if (control == null)
                return default(T);
            return (T)control;
        }


        protected childItem FindVisualChild<childItem>(DependencyObject obj)
            where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }
    }
}
